package cn.rengy.tool.core;

import cn.rengy.tool.core._Snowflake;
import cn.rengy.tool.core.util._DateUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 *
 * 雪花算法生成id，单机实现
 *  自定义实现可参考https://github.com/baidu/uid-generator/blob/master/README.zh_cn.md
 * @author rengy
 *
 *
 */
@Slf4j
//@DS("config")
public class IdGenerator {

    private static _Snowflake snowflake;

    private IdGenerator() {

    }

    public static _Snowflake getSnowflake(){
        return snowflake;
    }
    private static boolean isInit=false;
    public static void init(int startNumber, int workerId){
        if(!isInit){
            snowflake=new _Snowflake(workerId,startNumber,true);
            isInit=true;
        }
    }

    //private String ip;
    //private String hostname;



    public static Long nextId(){
        return snowflake.nextId();
    }
    public static Long createId(Date time){
        return snowflake.createId(time.getTime());
    }
    public static String parseId(Long id){
        return _DateUtils.formatDate(snowflake.getGenerateDateTime(id), null, "yyyy-MM-dd HH:mm:ss");
    }
    public static String nowDate() {
        return snowflake.getSnowflakeClockTime();
    }


    public static void main(String[] args) {
        _Snowflake snowflake=new _Snowflake(0,0,true);

        for(int i=0;i<50;i++) {
            System.out.println(snowflake.nextId());
        }
        //System.out.println(snowflake.getSnowflakeClockTime());

//
//		System.out.println(snowflake.getSnowflakeClockTime());
//		for(int i=0;i<131076;i++) {
//			long id=snowflake.nextId();
//			System.out.println(id+"="+snowflake.getDataCenterId(id)+"|"+snowflake.getGenerateDateTime(id));
//		}
//		System.out.println(snowflake.getSnowflakeClockTime());
//		long s=-1L ^ (-1L << 41);
//		//System.out.println(s);
//		long n=365L*24l*60l*60l*1000l;
//		//System.out.println(s/n);
//		long timestamp=System.currentTimeMillis();
//		long twepoch=1610887976000L;
//		System.out.println(-1L ^ (-1L << 7));
//		System.out.println(-1L ^ (-1L << 3));
//		System.out.println("=========================");
//		long sequenceBits = 12L;
//		long workerIdBits = 7L;
//		long dataCenterIdBits = 3L;
//		long workerId=0;
//		long dataCenterId=0;
//		long timestampLeftShift = sequenceBits + workerIdBits + dataCenterIdBits;
//		long dataCenterIdShift = sequenceBits + workerIdBits;
//		long sequence = 0L;
//		long workerIdShift = sequenceBits;
//		long id=((timestamp - twepoch) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) | (workerId << workerIdShift) | sequence;
//		System.out.println(id);
//		long maxDataCenterId=-1L ^ (-1L << dataCenterIdBits);
//		for(int i=0;i<20;i++) {
//			dataCenterId=i%maxDataCenterId;
//			System.out.println(dataCenterId);
//			id=((timestamp - twepoch) << timestampLeftShift) | (dataCenterId << dataCenterIdShift) | (workerId << workerIdShift) | sequence;
//			System.out.println(id);
//		}
//		System.out.println(LocalDateTime.now());
//		System.out.println(LocalTime.now().getSecond());
    }
}
